# editcarddialog.py
# this is a dialog to allow the user to edit a card.

import sys
from PyQt4 import QtGui, QtCore
from flashcard import *

class EditCardDialog(QtGui.QDialog):
    def __init__(self, card, parent = None):
        QtGui.QDialog.__init__(self, parent)
        
        self.card = card
        #print self.card.values['english']
        
        self.setWindowTitle('Edit Card')
        self.layout = QtGui.QHBoxLayout(self)
        self.columns = [QtGui.QVBoxLayout(), QtGui.QVBoxLayout()]
        self.handles = {} # handles into the contents of the widgets on this dialog
        
        self.typeSelect = QtGui.QComboBox(self)
        self.typeSelect.addItems(self.card.vocab.wordtypes.keys())
        self.previousType = None
            
        for i in range(0, self.typeSelect.count()):
            if self.card.wordType.name == self.typeSelect.itemText(i):
                self.typeSelect.setCurrentIndex(i)
        self.columns[0].addWidget(self.typeSelect)
        
        columnIndex = 1
        for field in self.card.vocab.wordtypes[str(self.typeSelect.currentText())].fields:
            (editWidget, self.handles[field.name]) = field.generateEditLayout(self.card.values[field.name])
            self.columns[columnIndex].addLayout(editWidget)
            columnIndex += 1
            if columnIndex >= len(self.columns):
                columnIndex = 0
        
        self.okButton = QtGui.QPushButton('Save', self)
        self.cancelButton = QtGui.QPushButton('Cancel', self)
        self.columns[0].addWidget(self.okButton)
        self.columns[1].addWidget(self.cancelButton)
        
        for column in self.columns:
            self.layout.addLayout(column)
        
        self.connect(self.typeSelect, QtCore.SIGNAL('currentIndexChanged(int)'), self.changeWordType)
        self.connect(self.okButton, QtCore.SIGNAL('clicked()'), self.ok)
        self.connect(self.cancelButton, QtCore.SIGNAL('clicked()'), self.cancel)
    
    def changeWordType(self, index):
        if self.previousType == None:
            self.previousType = self.card.wordType
        self.card.wordType = self.card.vocab.wordtypes[str(self.typeSelect.currentText())]
        for field in self.card.wordType.fields:
            if field.name not in self.card.values.keys():
                self.card.values[field.name] = ''
        #if len(self.card.guiArea) > 0:
         #   self.card.redoLayout()
        # now need to change the fields in this dialog to reflect new wordtype
        # easiest way to do this (but probably not the best) is close this dialog and open a new one.
        if self.__class__.__name__ == "EditCardDialog":
            self.card.vocab.dialog = EditCardDialog(self.card, self.card.vocab.mainGui)
        else:
            self.card.vocab.dialog = NewCardDialog(self.card.vocab, self.card, self.card.vocab.mainGui)
        self.card.vocab.dialog.show()
        self.close()
    
    def ok(self):
        values = {}
        for fieldName in self.handles:
            values[fieldName] = unicode(self.handles[fieldName].text())
        self.card.values = values
        self.card.redoLayout()
        self.card.vocab.cardChanged(self.card)
        #newCard = FlashCard(self.card.vocab, self.card.vocab.mainGui, str(self.typeSelect.currentText()), values)
        #self.card = newCard
        #self.card.vocab.replaceCard(self.card, newCard)
        #if self.card.vocab.isActive(newCard):
        #    self.card.vocab.activeCards.setCurrentWidget(newCard)
        #    self.card.vocab.resetCurrentCard()
        self.card.vocab.changed = True
        self.close()
    
    def cancel(self):
        if not self.previousType == None:
            self.card.wordType = self.previousType
       # if len(self.card.guiArea) > 0:
       #     self.card.redoLayout()
        self.close()

class NewCardDialog(EditCardDialog):
    def __init__(self, vocab, card = None, parent = None):
        newCard = None
        if (card == None):
            newCard = FlashCard(vocab, vocab.mainGui, vocab.wordtypes.keys()[0], vocab.defaultCardValues)
        else:
            newCard = card.copy()
        EditCardDialog.__init__(self, newCard, parent)
        self.setWindowTitle('New Card')
        self.againButton = QtGui.QPushButton('Save and add another', self)
        self.columns[0].addWidget(self.againButton)
        self.connect(self.againButton, QtCore.SIGNAL('clicked()'), self.again)
    
    def ok(self):
        self.card.vocab.cards.append(self.card)
        if self.card.vocab.activeCards.count() > 0:
            self.card.vocab.activeCards.insertWidget(self.card.vocab.activeCards.currentIndex() + 1, self.card)
            self.card.vocab.progress.setRange(0, self.card.vocab.progress.maximum() + 1)
        EditCardDialog.ok(self)
        self.card.vocab.mainGui.setCardActionsEnabled(True)
    
    def again(self):
        self.ok()
        #QtGui.QMessageBox.information(self, 'Card saved', 'Card saved', QtGui.QMessageBox.Ok)
        self.card.vocab.newCard(self.card)
    
    def cancel(self):
        self.close()

